package com.parktech.sun.domain.service.unionnews.impl;

import com.parktech.sun.domain.model.ContentEntity;
import com.parktech.sun.domain.model.unionnews.UnionNews;
import com.parktech.sun.domain.model.unionnews.UnionNewsGroup;
import com.parktech.sun.domain.repository.Page;
import com.parktech.sun.domain.repository.unionnews.UnionNewsRepository;
import com.parktech.sun.domain.service.impl.BaseServiceImpl;
import com.parktech.sun.domain.service.unionnews.UnionNewsService;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Property;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * User: Think
 * Date: 11-1-19
 * Time: 上午2:06
 */
@Service
public class UnionNewsServiceImpl extends BaseServiceImpl<UnionNews> implements UnionNewsService {

    private UnionNewsRepository unionNewsRepository;

    @Autowired
    public UnionNewsServiceImpl(UnionNewsRepository unionNewsRepository) {
        super(unionNewsRepository);
        this.unionNewsRepository = unionNewsRepository;
    }

    public Page search(String title, Integer publishStatus, Page page) {

        Criteria criteria = unionNewsRepository.createCriteria();

        if (StringUtils.isNotEmpty(title)) {
            criteria.add(Property.forName("title").like(title, MatchMode.ANYWHERE));
        }
        if (publishStatus != -1) {
            criteria.add(Property.forName("publishStatus").eq(publishStatus));
        }
        criteria.addOrder(Order.desc("orderCode"));
        criteria.addOrder(Order.desc("publishDate"));
        criteria.addOrder(Order.desc("modifyTime"));
        return unionNewsRepository.page(criteria, page.getPageNo(), page.getPageSize());
    }

    public Page searchContentEntity(String title, Page page) {

        Criteria criteria = unionNewsRepository.createCriteria(ContentEntity.class);

        if (StringUtils.isNotEmpty(title)) {
            criteria.add(Property.forName("title").like(title, MatchMode.ANYWHERE));
        }
        criteria.add(Property.forName("publishStatus").eq(1));
        criteria.addOrder(Order.desc("orderCode"));
        criteria.addOrder(Order.desc("publishDate"));
        criteria.addOrder(Order.desc("modifyTime"));

        return unionNewsRepository.page(criteria, page);
    }

    public UnionNewsGroup getGroupById(Long groupId) {
        return unionNewsRepository.getGroupById(groupId);
    }

    public void saveOrUpdateGroup(UnionNewsGroup unionNewsGroup) {
        unionNewsRepository.saveOrUpdateGroup(unionNewsGroup);
    }

    public Page page(Page page) {

        Criteria criteria = unionNewsRepository.createCriteria();

        criteria.add(Property.forName("publishStatus").eq(1));
        criteria.addOrder(Order.desc("orderCode"));
        criteria.addOrder(Order.desc("publishDate"));
        criteria.addOrder(Order.desc("modifyTime"));

        return unionNewsRepository.page(criteria, page);
    }

    public UnionNews getTop() {

        Criteria criteria = unionNewsRepository.createCriteria();

        criteria.add(Property.forName("publishStatus").eq(1));
        criteria.addOrder(Order.desc("orderCode"));
        criteria.addOrder(Order.desc("publishDate"));
        criteria.addOrder(Order.desc("modifyTime"));

        criteria.setMaxResults(1);

        List result = criteria.list();

        if (result == null || result.size() == 0) {
            return null;
        }
        return (UnionNews) result.get(0);
    }
}
